﻿using System.Text;
using System.Windows;
using PSE.Deployment.Engine;
using PSE.Deployment.Engine.Enums;
using PSE.Deployment.Engine.Interfaces;
using System.Windows.Forms;

namespace PSE.Deployment.SetupBase.UI
{
    public partial class InstallationReport : BaseWindow
    {
        public StepCollection IgnoredSteps
        {
            get { return (StepCollection)GetValue(IgnoredStepsProperty); }
            set { SetValue(IgnoredStepsProperty, value); }
        }

        public static readonly DependencyProperty IgnoredStepsProperty =
            DependencyProperty.Register("IgnoredSteps", typeof(StepCollection), typeof(InstallationReport), new UIPropertyMetadata(null));

        public Parameter.Enums.SetupMode SetupMode { get; set; }

        public InstallationReport()
        {
            this.InitializeComponent();
            this.DataContext = this;
            this.Loaded += new RoutedEventHandler(InstallationReport_Loaded);
        }

        private void InstallationReport_Loaded(object sender, RoutedEventArgs e)
        {
            switch (SetupMode)
            {
                case PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Install:
                case PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.HotFix:
                case PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.ServicePack:
                    txtReportTitle.Content = "Instalação";
                    break;
                case PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Uninstall:
                    txtReportTitle.Content = "Desinstalação";
                    break;
                case PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Repair:
                    txtReportTitle.Content = "Reparação";
                    break;
                case PSE.Deployment.SetupBase.Parameter.Enums.SetupMode.Change:
                    txtReportTitle.Content = "Adicionar / Remover Componentes";
                    break;
            }
        }

        private void BtnNext_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationAction = StepNavigationAction.Next;
            this.Close();
        }

        private void BtnCopyError_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            try
            {
                IStep step = this.lvErrors.SelectedValue as IStep;
                if (step != null)
                {
                    sb.AppendLine("Passo:" + step.Description);
                    sb.AppendLine("Erro:" + step.ExecutionException.ToString());
                    System.Windows.Clipboard.SetText(sb.ToString());
                }
            }
            catch
            {
                SetupMainFactory.GetInstance().WriteStepLog(string.Format("Não foi copiar o texto {0} para a área de transferência. ", sb.ToString()));
            }
        }

        protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Key == System.Windows.Input.Key.Enter)
            {
                this.Close();
            }
        }
    }
}